<!-- 设备运行监测 -->
<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { ref, unref, reactive } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { Search } from '@/components/Search'
import { Detail } from './components'
import { Dialog } from '@/components/Dialog'
import { busFacilityPageDataList } from '@/api/highway'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import { Icon } from '@/components/Icon'
import { onlineStateOptions } from '@/assets/data/deviceData'

const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await busFacilityPageDataList(
      {
        pageNum: unref(currentPage),
        pageSize: unref(pageSize)
      },
      {
        ...unref(searchParams)
      }
    )
    return {
      list: res.data.records || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods
const marginR = '25px'
// 设备运行监测列表:所属地市、所属路段、设备名称、设备类型、设备编号、物理位置、所属业务系统、设备IP、主DNS、运行状态、最后监测时间
// 查询:设备类型、设备名称、设备IP
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('userDemo.index'),
    search: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      type: 'index'
    }
  },
  {
    field: 'city',
    label: '所属地市',
    search: {
      hidden: true
    }
  },
  {
    field: 'road',
    label: '所属路段',
    search: {
      hidden: true
    }
  },
  {
    field: 'name',
    label: '设备名称'
  },
  {
    field: 'category',
    label: '设备类型'
  },
  {
    field: 'deviceNo',
    label: '设备编号',
    search: {
      hidden: true
    }
  },
  {
    field: 'physicalLocation',
    label: '物理位置',
    table: {
      slots: {
        default: () => <Icon icon="vi-ep:location-filled" class="cursor-pointer" />
      }
    },
    search: {
      hidden: true
    }
  },
  {
    field: 'businessSystem',
    label: '所属业务系统',
    search: {
      hidden: true
    }
  },
  {
    field: 'ip',
    label: '设备IP'
  },
  {
    field: 'primaryDns',
    label: '主DNS',
    search: {
      hidden: true
    }
  },
  {
    field: 'onlineState',
    label: '运行状态',
    table: {
      slots: {
        default: ({ row }) => {
          const value = row.onlineState
          if (value !== undefined && value !== null) {
            return <span>{onlineStateOptions[value].label}</span>
          } else {
            return <span></span>
          }
        }
      }
    },
    detail: {
      slots: {
        default: (data: any) => {
          const value = data.onlineState
          if (value !== undefined && value !== null) {
            return <span>{onlineStateOptions[value].label}</span>
          } else {
            return <span></span>
          }
        }
      }
    },
    search: {
      hidden: true
    }
  },
  {
    field: 'lastConnect',
    label: '最后监测时间',
    search: {
      hidden: true
    }
  },

  {
    field: 'action',
    label: t('userDemo.action'),
    detail: {
      hidden: true
    },
    search: {
      hidden: true
    },
    table: {
      width: 180,
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <BaseButton type="success" link onClick={() => action(row)}>
                {t('exampleDemo.detail')}
              </BaseButton>
              <BaseButton type="primary" link onClick={() => actionMaintain(row)}>
                维修处理
              </BaseButton>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  console.log('params', params)

  currentPage.value = 1
  if (params.createTime && params.createTime.length === 2) {
    params = { ...params, startTime: params.createTime[0], endTime: params.createTime[1] }
  }
  console.log('params', params)
  searchParams.value = params
  getList()
}

const action = (row: any) => {
  dialogTitle.value = t('exampleDemo.detail')
  currentRow.value = row
  dialogVisible.value = true
}

const actionMaintain = (row: any) => {
  dialogTitle.value = '维修处理'
  // TODO: 维修处理弹框，就是设备检定维护的新增弹窗

  currentRow.value = row
  //   dialogVisible.value = true
}

const dialogVisible = ref(false)
const dialogTitle = ref('')

const currentRow = ref<any>()
</script>

<template>
  <ContentWrap class="flex-[1]">
    <Search :schema="allSchemas.searchSchema" @reset="setSearchParams" @search="setSearchParams" />

    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      @register="tableRegister"
      :pagination="{
        total
      }"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Detail :detail-schema="allSchemas.detailSchema" :current-row="currentRow" />
    <template #footer>
      <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
    </template>
  </Dialog>
</template>
